package gw.com.android.ui.trade;

import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.gwtsz.gts2.hx.R;

import gw.com.android.ui.views.CommonTitleBar;
import www.com.library.dialog.BasePopWindow;
import www.com.library.util.CommonUtils;
import www.com.library.view.BtnClickListener;
import www.com.library.view.RecycleViewDivider;
import www.com.library.view.RecyclerClickListener;
import www.com.library.view.TintTextView;

/**
 * 挂单界面时下单类型弹层
 * @author Administrator
 *
 */
public class TradeTypePopWindow extends BasePopWindow {

	private String[] mStringArray;// 保存从接口获取到的最原始的数据
	private int mSelectedType = 0;
	private CommonTitleBar mTitleBar;
	private String mTitle = "";

	public TradeTypePopWindow(Activity activity, View view,String title,int selectType,String[] array, RecyclerClickListener callback) {
		this.mTitle = title;
		this.mSelectedType = selectType;
		mStringArray = array;
		initPopWindow(activity, view, R.layout.order_product_list,callback);
	}

	@Override
	public void createPopWindow(View dialogView) {
		mTitleBar = (CommonTitleBar) dialogView.findViewById(R.id.common_title_bar);
		mTitleBar.setAppTitle(mTitle);
		
		mTitleBar.setBtnClickListener(new BtnClickListener() {
			
			@Override
			public void onBtnClick(int viewID) {
				if(viewID == R.id.title_left_icon){
                	hidden();
                }
			}
		});
		
		LinearLayout listLayout = (LinearLayout) dialogView.findViewById(R.id.list_layout);
		popListView = (RecyclerView) dialogView.findViewById(R.id.pop_list);
		// 线性布局管理器
		LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
		// 设置布局管理器
		popListView.setLayoutManager(linearLayoutManager);
		popListView.addItemDecoration(new RecycleViewDivider(activity, LinearLayoutManager.VERTICAL));
		modeListAdapter = new TradeTypeAdapter(activity);
		popListView.setAdapter(modeListAdapter);

		listLayout.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				return true;
			}
		});
	}

	@Override
	public void show() {
		// super.show();
		if (hasShowing() || modeListAdapter.getItemCount() < 1) {
			return;
		}
		if (!activity.isFinishing()) {
			view = activity.getWindow().getDecorView();
			popWindow.showAtLocation(view, Gravity.FILL, 0, 0);
		}
	}
	
	/**
	 * 交易类型列表适配器
	 * @author Administrator
	 *
	 */
	public class TradeTypeAdapter extends RecyclerView.Adapter{
		private LayoutInflater mInflater;

		public TradeTypeAdapter(Activity act) {
			mInflater = LayoutInflater.from(act);
		}
		
		/** 
		 * 刷新列表
		 * @param selectedType
		 */
		public void refreshData(int selectedType){
			mSelectedType = selectedType;
			notifyDataSetChanged();
		}

		public String getItem(int position) {
			if (position < 0 || position >= mStringArray.length)
				return null;
			return mStringArray[position];
		}

		@Override
		public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
			View itemView = mInflater.inflate(R.layout.list_item_select, parent,false);
			return new ItemView(itemView);
		}

		@Override
		public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
			ItemView viewHold = (ItemView) holder;
			if (viewHold.radio != null) {
				String mValue = getItem(position);
				viewHold.radio.setText(mValue);
				viewHold.radio.setTag(position);

				if (mSelectedType == position){
					viewHold.radio.setSelected(true);
				}else{
					viewHold.radio.setSelected(false);
				}
			}
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public int getItemCount() {
			if(null != mStringArray){
				return mStringArray.length;
			}
			return 0;
		}

		public class ItemView extends RecyclerView.ViewHolder implements View.OnClickListener{

			public TintTextView radio;

			public ItemView(View itemView) {
				super(itemView);
				radio = (TintTextView) itemView.findViewById(R.id.item_select_type);
				radio.setOnClickListener(this);
			}

			@Override
			public void onClick(View v) {

				if(CommonUtils.isFastDoubleClick()){
					return;
				}
				if(null != callback){
					int position = (int) v.getTag();
					hidden();
					popWindow.setFocusable(false);
					popWindow.update();
					((TradeTypeAdapter) modeListAdapter).refreshData(position);
					callback.onClick(position, null);
				}
			}
		}
	}
}
